<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="医生姓名" prop="doctorName">
				<el-input
					v-model="queryParams.doctorName"
					placeholder="请输入医生姓名"
					clearable
					size="small"
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="医院名称" prop="hospitalName">
				<el-input
					v-model="queryParams.hospitalName"
					placeholder="请输入医院名称"
					clearable
					size="small"
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="部门名称" prop="deptName">
				<el-input
					v-model="queryParams.deptName"
					placeholder="请输入部门名称"
					clearable
					size="small"
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="申请状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择申请状态" clearable size="small">
					<el-option label="申请中" :value="0"/>
					<el-option label="通过" :value="1"/>
					<el-option label="拒绝" :value="2"/>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
<!--			<el-col :span="1.5">
				<el-button
					type="primary"
					icon="el-icon-plus"
					size="mini"
					@click="handleAdd"
				>新增
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="success"
					icon="el-icon-edit"
					size="mini"
					:disabled="single"
					@click="handleUpdate"
				>修改
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="danger"
					icon="el-icon-delete"
					size="mini"
					:disabled="multiple"
					@click="handleDelete"
				>删除
				</el-button>
			</el-col>-->
			<el-col :span="1.5">
				<el-button
					type="warning"
					icon="el-icon-download"
					size="mini"
					@click="handleExport"
				>导出
				</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center"/>
			<el-table-column type="index" :index="indexMethod" label="序号" width="55" align="center"/>
			<el-table-column label="医生姓名" align="center" prop="doctorName"/>
			<el-table-column label="医院名称" align="center" prop="hospitalName"/>
			<el-table-column label="部门名称" align="center" prop="deptName"/>
			<el-table-column label="申请图" align="center" width="150">
				<template slot-scope="scope">
					<el-image :src="ossUrl + scope.row.applyImg" :preview-src-list="[ossUrl + scope.row.applyImg]"></el-image>
				</template>
			</el-table-column>
			<el-table-column label="申请附件" align="center" prop="applyFile"/>
			<el-table-column label="申请说明" :show-overflow-tooltip="true" align="center" prop="applyRemark"/>
			<el-table-column label="申请状态" align="center" prop="status">
				<template slot-scope="scope">{{ scope.row.status | statusFilter }}</template>
			</el-table-column>
			<el-table-column label="操作" align="center">
				<template slot-scope="scope">
					<el-button
						size="mini"
						type="text"
						icon="el-icon-success"
						:disabled="scope.row.status != 0"
						@click="updateApply(scope.row.applyId, 1, scope.row.hospitalId)"
					>通过
					</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-error"
						:disabled="scope.row.status != 0"
						@click="updateApply(scope.row.applyId, 2)"
					>拒绝
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>

		<!-- 添加或修改加入活动申请对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="医生id" prop="doctorId">
					<el-input v-model="form.doctorId" placeholder="请输入医生id"/>
				</el-form-item>
				<el-form-item label="医生姓名" prop="doctorName">
					<el-input v-model="form.doctorName" placeholder="请输入医生姓名"/>
				</el-form-item>
				<el-form-item label="医院id" prop="hospitalId">
					<el-input v-model="form.hospitalId" placeholder="请输入医院id"/>
				</el-form-item>
				<el-form-item label="医院名称" prop="hospitalName">
					<el-input v-model="form.hospitalName" placeholder="请输入医院名称"/>
				</el-form-item>
				<el-form-item label="部门id" prop="deptId">
					<el-input v-model="form.deptId" placeholder="请输入部门id"/>
				</el-form-item>
				<el-form-item label="部门名称" prop="deptName">
					<el-input v-model="form.deptName" placeholder="请输入部门名称"/>
				</el-form-item>
				<el-form-item label="申请图" prop="applyImg">
					<el-input v-model="form.applyImg" placeholder="请输入申请图"/>
				</el-form-item>
				<el-form-item label="申请附件" prop="applyFile">
					<el-input v-model="form.applyFile" placeholder="请输入申请附件"/>
				</el-form-item>
				<el-form-item label="申请附件" prop="applyRemark">
					<el-input v-model="form.applyRemark" placeholder="请输入申请附件"/>
				</el-form-item>
				<el-form-item label="申请状态">
					<el-radio-group v-model="form.status">
						<el-radio label="1">请选择字典生成</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import {listApply, getApply, delApply, addApply, updateApply, exportApply} from "@/api/apply/apply";
import { updateHospital } from "@/api/hospital/hospital";

export default {
	name: "Apply",
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 加入活动申请表格数据
			applyList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				doctorId: null,
				doctorName: null,
				hospitalId: null,
				hospitalName: null,
				deptId: null,
				deptName: null,
				applyImg: null,
				applyFile: null,
				applyRemark: null,
				status: null,
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {},
			baseUrl: this.$baseUrl,
			ossUrl: this.$ossUrl
		};
	},
	created() {
		this.getList();
	},
	methods: {
		updateApply(applyId, status, hospitalId) {
			this.$confirm('是否确定执行该操作?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				updateApply({applyId: applyId, status: status}).then(res => {
					if(res.code == 200) {
						if(status == 1) {
							updateHospital({hospitalId: hospitalId, status: 'enable'});
						}
						this.msgSuccess("修改成功");
						this.getList();
					}
				})
			});
		},
		/** 查询加入活动申请列表 */
		getList() {
			this.loading = true;
			listApply(this.queryParams).then(response => {
				this.applyList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				applyId: null,
				doctorId: null,
				doctorName: null,
				hospitalId: null,
				hospitalName: null,
				deptId: null,
				deptName: null,
				applyImg: null,
				applyFile: null,
				applyRemark: null,
				status: 0,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.applyId)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加加入活动申请";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const applyId = row.applyId || this.ids
			getApply(applyId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改加入活动申请";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.applyId != null) {
						updateApply(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							}
						});
					} else {
						addApply(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							}
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const applyIds = row.applyId || this.ids;
			this.$confirm('是否确认删除加入活动申请编号为"' + applyIds + '"的数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return delApply(applyIds);
			}).then(() => {
				this.getList();
				this.msgSuccess("删除成功");
			}).catch(function () {
			});
		},
		/** 导出按钮操作 */
		handleExport() {
			const queryParams = this.queryParams;
			this.$confirm('是否确认导出所有加入活动申请数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return exportApply(queryParams);
			}).then(response => {
				this.download(response.msg);
			}).catch(function () {
			});
		},
		indexMethod(index) {
			return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
		}
	},
	filters: {
		statusFilter: (text) => {
			if(text == 0) {
				return '申请中'
			} else if(text == 1) {
				return '通过'
			} else {
				return '拒绝'
			}
		}
	}
};
</script>
